﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Telerik.Web.Mvc;

using MyVoteMVC.Domain;
using MyVoteMVC.Service;
using MyVoteMVC.Common;
using MyVoteMVC.Web.Extensions;
using MyVoteMVC.Web.Models;

namespace MyVoteMVC.Web.Controllers
{
    public class ProjectController : VoteBaseController
    {
        /// <summary>
        /// 从列表进入活动，查看人数加一，重定向到Details
        /// </summary>
        /// <param name="id">ActivityId</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult DetailsByActivityID(string id)
        {
            using (IActivityService pService = Factory.GetInstance<IActivityService>())
            {
                var pResult = pService.AddActivityViewCount(id);
                //var pQuestions = pResult.Questions;
                return RedirectToAction("Details", new { id = id });
            }
        }

        /// <summary>
        /// 根据ActivityId返回VT_Activities实体
        /// </summary>
        /// <param name="id">ActivityId</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Details(string id)
        {
            FormsIdentity pIdentity = (FormsIdentity)HttpContext.User.Identity;
            FormsAuthenticationTicket pTicket = pIdentity.Ticket;

            ViewData["ticketSubmit"] = true;
            string pVoteTicket = string.Empty;

            //读取已保存票据信息
            using (ITickService pTickService = Factory.GetInstance<ITickService>())
            {
                ViewData["ticketSubmit"] = pTickService.IsCanModify(id, pTicket.UserData);
            }

            using (IProjectService pProjectService = Factory.GetInstance<IProjectService>())
            {
                ViewData["typeList"] = pProjectService.GetProjectsTypeByActivity(id);
                ViewData["yearList"] = pProjectService.GetProjectsYearGroupByActivity(id);
            }

            using (IActivityService pService = Factory.GetInstance<IActivityService>())
            {
                var pActivity = pService.GetActivities(id);
                var pQuestions = pActivity.Questions;
                if (pActivity.Activity_EndTime < DateTime.Now)
                    ViewData["ticketSubmit"] = false;
                return View(pActivity);
            }
        }        

        [GridAction]
        public ActionResult _ListItemByQuestionId(string id)
        {
            //读取用户身份
            FormsIdentity pIdentity = (FormsIdentity)HttpContext.User.Identity;
            FormsAuthenticationTicket pTicket = pIdentity.Ticket;

            using (IQuestionService pService = Factory.GetInstance<IQuestionService>())
            {
                ProjectItemConverter pConverter = new ProjectItemConverter();
                var pQuestion = pService.GetQuestion(id);
                var pList = pQuestion.Items;
                var pResult = pList.ToModelList<VT_Items, ProjectItem>(pConverter);

                using (ITickService pTickService = Factory.GetInstance<ITickService>())
                {
                    string pTicketContent = pTickService.GetContent(pQuestion.Activity_ID, pTicket.UserData);
                    if (!string.IsNullOrEmpty(pTicketContent))
                    {
                        foreach (string pContent in pTicketContent.Split('&'))
                        {
                            if (pResult.Any(o => o.ID == pContent.Split('=')[1]))
                            {
                                pResult.FirstOrDefault(o => o.ID == pContent.Split('=')[1]).Selected = true;
                            }
                        }
                    }
                }
                return View(new GridModel(pResult));
            }
        }

        public ActionResult ProjectInfoByItemId()
        {
            string Query = Request.QueryString.ToString();
            using (IItemService pService = Factory.GetInstance<IItemService>())
            {
                var project = new ProjectInfoConverter().Convert(pService.GetItems(Query));
                return PartialView("ProjectInfoControl", project);
            }
        }

    }
}
